package com.sxx.beautifulcity.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.adapter.PetAdapter;
import com.sxx.beautifulcity.entity.DeliciousFoodData;
import com.sxx.beautifulcity.entity.PetData;
import com.sxx.beautifulcity.entity.SpecialityData;
import com.sxx.beautifulcity.entity.Tuan_list_Tongyong;
import com.sxx.beautifulcity.entity.greendao.AgritourismDaoEntity;
import com.sxx.beautifulcity.entity.greendao.AroundDaoEntity;
import com.sxx.beautifulcity.entity.greendao.HolidayDaoEntity;
import com.sxx.beautifulcity.entity.greendao.PluckDaoEntity;
import com.sxx.beautifulcity.entity.greendao.VillageDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;
import com.sxx.beautifulcity.constant.Constant;
import com.sxx.beautifulcity.utils.VolleySingleton;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/23.
 */
public class PetActivity extends DialogBaseActivity implements View.OnClickListener,
        OnMyItemClickListener {
    private TextView titlrTv , moreTv;
    private RecyclerView recyclerView;
    private ImageView backIv;
    private PetAdapter adapter;
    private final static String TAG = "PetActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview_pet);
        init();
        requestPetContent();
    }

    /***
     * 初始化数据
     */
    private void init() {
        titlrTv = (TextView) findViewById(R.id.titleTv);
        titlrTv.setText("宠物");
        backIv = (ImageView) findViewById(R.id.backIV);
        backIv.setOnClickListener(this);
        moreTv = (TextView) findViewById(R.id.moreTv);
        moreTv.setText("");

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview_pet);
        adapter = new PetAdapter();
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);

        adapter.setOnMyItemClic(this);
    }

    /***
     * 请求网络数据
     */
    private void requestPetContent() {
        showDialog();
        String path = Constant.PET_URL;
        JsonObjectRequest request = new JsonObjectRequest(path, null,
                new Response.Listener<JSONObject>() {

                    Gson gson = new Gson();
                    List<PetData> datas = new ArrayList<>();
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("list_data")) {
                            try {
                                JSONArray list_data = response.getJSONArray("list_data");
                                for (int i = 0; i < list_data.length(); i++) {
                                    String str = list_data.getString(i);
                                    PetData data = gson.fromJson(str , PetData.class);
                                    datas.add(data);
                                }
                                    adapter.addData(datas);
                                dismissDialog();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        VolleySingleton.getInstance().addRequest(request, TAG);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this , MainActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.left_out, R.anim.right_into);
    }

    @Override
    public void onMyItemClick(Tuan_list_Tongyong data) {

    }

    @Override
    public void onMyItemClick(AroundDaoEntity data) {

    }

    @Override
    public void onMyItemClick(HolidayDaoEntity data) {

    }

    @Override
    public void onMyItemClick(AgritourismDaoEntity data) {

    }


    @Override
    public void onMyItemClick(PluckDaoEntity data) {

    }

    @Override
    public void onMyItemClick(VillageDaoEntity data) {

    }
    @Override
    public void onMyItemClick(SpecialityData data) {

    }

    @Override
    public void onMyItemClick(DeliciousFoodData data) {

    }

    @Override
    public void onMyItemClick(PetData data) {
        Intent intent = new Intent(this , PetDetailActivity.class);
        intent.putExtra("id" , data.getId());
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.left_out, R.anim.right_into);
    }
}
